<?php


namespace App\Helpers;


use App\Models\ActionLog;
use Carbon\Carbon;

trait CommonTrait
{

    //后台日志
    protected function actionLog($content, $uid = 0)
    {
        if (is_array($content) && !empty($content)) {
            $data = [];
            foreach ($content as $k => $v) {
                $data[] = [
                    'uid' => $uid,
                    'content' => $v,
                    'ip' => request()->ip(),
                    'created_at' => Carbon::now()->toDateTimeString(),
                    'updated_at' => Carbon::now()->toDateTimeString()
                ];
            }
            if (!ActionLog::query()->insert($data)) {
                return false;
            }
            return true;
        } else {
            $data = [
                'uid' => $uid,
                'content' => $content,
                'ip' => request()->ip(),
                'created_at' => Carbon::now()->toDateTimeString(),
                'updated_at' => Carbon::now()->toDateTimeString()
            ];
            if (!ActionLog::insert($data)) {
                return false;
            }
            return true;
        }
    }
    // strToArray
    protected function strToArray ($str) {
        if(is_array($str)){
            return $str;
        }else{
            return json_decode($str,true);
        }
    }
}
